﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpBolt.Graphics.GraphicSources;
using InductBase;
using System.ComponentModel;
using Newtonsoft.Json;

namespace SharpBolt.Graphics.GraphicInstances
{
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class RasterFontInstance : GraphicInstance
    {
        public double RowShift { get; set; }
        public string Text{ get; set; }
        public event EventHandler<RasterFontGliphArgs> GliphRender;

        [JsonIgnore]
        RasterFontSourse RasterFontSource { get; set; }

        public RasterFontInstance(RasterFontSourse source)
            : base()
        {
            RasterFontSource = source;
            _size = Vec2.Unit*100;
            RowShift = 0;
        }

        public override void Draw(Painter painter)
        {
            if (Hidden) return;

            Rec textArea = Rec.FromLocationSize(Transformation.Position, Size * Transformation.Scale);           
            string text = Text;// Source.SplitByWords(textArea, Text, Transformation.Scale);
            if (textArea.Area < 0)                
                textArea.Size = new Vec2(0, 0);

            Action<RasterFontGliphArgs> gliphRender = null;

            if (GliphRender != null)
                gliphRender = p => GliphRender(this, p);

            RasterFontSource.Draw(
                painter,
                textArea.X,
                textArea.Y,
                textArea.Width,
                textArea.Height,
                RowShift,
                text,
                Color,
                Transformation.Scale,
                0,
                gliphRender
                );
        }

        private Vec2 _size;
        public override InductBase.Vec2 Size
        {
            get
            {
                return _size;
            }

            set
            {
                _size = value;
            }
        }
    }
}
